
package org.example.wu_and_dian.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.wu_and_dian.dto.request.UpdateProjectRequest;
import org.example.wu_and_dian.entity.Project;
import org.example.wu_and_dian.mapper.ProjectMapper;
import org.example.wu_and_dian.mapper.UserMapper;
import org.example.wu_and_dian.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Date;
import org.example.wu_and_dian.entity.User;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;

/**
 * @author SS2613
 * @description 针对表【project】的数据库操作Service实现
 * @createDate 2025-03-20 11:34:39
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project>
    implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Project getProjectById(String projectId) {
        return projectMapper.selectById(projectId);
    }


    @Override
    public List<Project> getProjectsByEmail(String email) {
        return projectMapper.getProjectsByEmail(email);
    }


    @Override
    public int deleteProjectById(String projectId) {
        return projectMapper.deleteProjectById(projectId);
    }

    @Override
    public int createProject(String email, String name, String description) {
        User user = userMapper.selectUserByEmail(email);
        Project project = new Project();
        project.setId(String.valueOf(IdWorker.getId()));
        project.setUid(String.valueOf(user.getId()));
        project.setName(name);
        project.setDescription(description);
        project.setCreatedAt(new Date());
        project.setUpdatedAt(new Date());
        int insert = projectMapper.insert(project);
        return insert;
    }

    @Override
    public int updateProject(String projectId, UpdateProjectRequest request) {
        return projectMapper.updateProject(projectId, request.getName(), request.getDescription());
    }

}